package com.wisecoach.gatewayplus.transaction.lockedtx;

import com.wisecoach.gatewayplus.transaction.AbstractSubmittedTxProcessor;
import com.wisecoach.gatewayplus.transaction.InvalidTransactionHandler;
import com.wisecoach.gatewayplus.transaction.SubmitTxContext;
import org.hyperledger.fabric.client.Status;

import static org.hyperledger.fabric.protos.peer.TxValidationCode.VALID;

/**
 * {@code @author:} wisecoach
 * {@code @date:} 2023/9/26 下午9:27
 * {@code @version:} 1.0.0
 */


public class LockedSubmittedTxProcessor extends AbstractSubmittedTxProcessor {

    private final InvalidTransactionHandler invalidTransactionHandler;
    private LockedTxResource resource;

    public LockedSubmittedTxProcessor(InvalidTransactionHandler invalidTransactionHandler) {
        this.invalidTransactionHandler = invalidTransactionHandler;
    }

    @Override
    protected void doProcess(SubmitTxContext ctx, Status status) {
        if (VALID.equals(status.getCode())) {
            LockedTxContext lockedCtx = (LockedTxContext) ctx;
            TxRWSet rwSet = lockedCtx.getRwSet();
            resource.unlock(rwSet);
        } else {
            invalidTransactionHandler.handle(ctx, status);
        }
    }

    public void setResource(LockedTxResource resource) {
        this.resource = resource;
    }
}
